package com.seashop.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seashop.entity.User;
import com.seashop.request.UpdateUserRequestParam;
import com.seashop.response.MemberLevelResponse;
import com.seashop.response.ResponseBase;
import com.seashop.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * <p>
 * 前台用户表 前端控制器
 * </p>
 *
 * @author moredln
 * @since 2020-11-22
 */
@RestController
@Api(tags = "用户中心")
@Slf4j
@Validated
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/getUserInfo")
    @ApiOperation(value = "获取用户信息")
    @ApiImplicitParam(value = "用户ID", name = "userId", dataType = "integer", required = true)
    public ResponseBase<User> getUserInfo(@NotBlank Integer userId) {
        User user = userService.getOne(new QueryWrapper<User>().eq("user_id", userId));
        if (null == user) {
            return ResponseBase.fail("this user not exsit");
        }
        return ResponseBase.success(user);
    }

    @GetMapping("/updateUserInfo")
    @ApiOperation(value = "更新用户信息")
    public ResponseBase<User> updateUserInfo(@RequestBody UpdateUserRequestParam param) {
        User user = userService.getOne(new QueryWrapper<User>().eq("user_id", param.getUserId()));
        if (null == user) {
            return ResponseBase.fail("this user not exsit");
        }
        // 更改用户信息
        int resultNum = userService.updateUserInfo(user, param);
        if (resultNum < 1) {
            return ResponseBase.fail();
        }
        return ResponseBase.success();
    }

    @GetMapping("/queryUserByReferralCode")
    @ApiOperation(value = "根据推荐码获取邀请用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "推荐码", name = "referralCode", dataType = "String", required = true),
            @ApiImplicitParam(value = "国家编码(TH:泰国;IN:印尼;ZH:中国;US:美国;UR:巴基斯坦;)", name = "countryCode", dataType = "string", required = true)
    })
    public ResponseBase<User> queryUserByReferralCode(@NotBlank String referralCode, @NotBlank String countryCode) {
        User user = userService.getOne(new QueryWrapper<User>().eq("referral_code", referralCode).eq("country_code", countryCode));
        if (null == user) {
            return ResponseBase.fail("referral user not exsit");
        }
        return ResponseBase.success(user);
    }

}

